
unemployment<-function(wage,params){
  return(sapply(wage,function(x) min(0.75*x*params$hours,params$UIcap))) #FINISH THIS - UI function
}




transfers<-function(t,SS,SS.spouse,grossworkinc,potentialwage,params,foodstamps,wagepars,spwagepars,type){
  if(SS==1){
    SSamount= max(0.9*min(params$kink1,potentialwage) + 
      0.32*min(max(0,potentialwage-params$kink1),params$kink2) +
      0.15*min(max(0,potentialwage-params$kink2),params$kink3),params$SSIval)
  }
  else SSamount=0
  if(SS.spouse==1) SSamount= SSamount+ 0.9*min(params$kink1,PermWage(t+23*params$agelength-1,spwagepars,type)*params$hours) + 
      0.32*min(max(0,PermWage(t+23*params$agelength-1,spwagepars,type)*params$hours-params$kink1),params$kink2) +
      0.15*min(max(0,PermWage(t+23*params$agelength-1,spwagepars,type)*params$hours-params$kink2),params$kink3)
  
  netinc<- max(grossworkinc*0.8-1608/params$agelength-12*175*params$nchildren[[type]][t]/params$agelength,0)
  if(grossworkinc > params$fsgross | netinc > params$fsnet) transfer<-0
  else transfer <-foodstamps[[type]][t]-(netinc)*0.3
  return(max(SSamount,transfer)) #
  #if it depends on PERMANENT wage, should be fixed for everyone.
  #FINISH THIS
}
lnwagenext<-function(wage,t,h,params,type){ #for now, no deterministic evolution in wages
  t<-t+23*params$agelength
  lognext <- log(wage)+params$wagereg[[type]]$age/params$agelength + (params$wagereg[[type]]$agesq/params$agelength^2)*((t+1)^2-t^2) + params$wagereg[[type]]$health[h]
  return(lognext)
}


flow<-function(c,work,worksp,single,health,sphealth,paramvals){
  U<-(((c*exp(paramvals[4]*work*(1-single)+
              paramvals[6]*work*(single)+
              (paramvals[8]*(health==2)+paramvals[9]*(health==3))*work+
              paramvals[10]*worksp+
              paramvals[12]*worksp*(sphealth==2)+
              paramvals[1]*(health==2) + 
              paramvals[2]*(health==3)+
              paramvals[3]*(sphealth==2))
        )^(1-params$gamma))/(1-params$gamma))
  U[which(is.na(U))]<- -Inf
  return(U)
}


expectV<-function(V,Wage,Asset,t,health,w,a2,emp,DI,fired,params,type){  
  CEV<-NULL #conditional expectations, given health
  #wage transitions: mean is a FUNCTION of last period wage, but this period health
  if(fired==0){
    for(h2 in 1:3){
      wageprob<-dnorm(log(Wage[t+1,]),lnwagenext(Wage[t,w],t,h2,params,type),params$wagevar)/sum(dnorm(log(Wage[t+1,]),lnwagenext(Wage[t,w],t,h2,params,type),params$wagevar))
      CEV[h2]<- wageprob%*%V[t+1,,a2,emp,DI,h2]
    }
  }
  if(fired==1){
    for(h2 in 1:3){
      wageprob<-dnorm(log(Wage[t+1,]),lnwagenext(Wage[t,w],t,h2,params,type),params$wagevar)/sum(dnorm(log(Wage[t+1,]),lnwagenext(Wage[t,w],t,h2,params,type),params$wagevar))
      CEV[h2]<- wageprob%*%sapply(1:dim(Wage)[2], function(wx) interpolate(Asset[t+1,a2]+unemployment(Wage[t+1,wx],params),Asset[t+1,],V[t+1,wx,,1,DI,h2]))
      
    }
  }
  EV<-params$health.prob[[type]][t,health,]%*%CEV
  
  return(EV)
}

# gaminterp<-function(assetval,Asset,Wage,V,t,a2,h2,params,type){
#   solutions<-data.table(asset=Asset[t,],
#                         V=V[t,,a2,1,2,h2])
#                         
# 
#   
#   workinterp<-predict(gam(V~te(asset,fx=TRUE,k=10,bs='cr'),data=solutions),
#                       data.table(asset=assetval))
#   print(workinterp)
# }

PermWage<- function(t,params,type) exp(params$wagereg[[type]]$cons+(params$wagereg[[type]]$age/params$agelength)*t+(params$wagereg[[type]]$agesq/params$agelength^2)*(t^2)) #MAKE THIS PERMANENT WAGE
